package com.bw.shopping.mvvm.viewmodel;

import android.os.Bundle;
import android.util.Log;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.field.UIField;
import com.bw.mvvm_common.mvvm.utils.GreenDaoUtils;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.shopping.adapter.OrderAdapter;
import com.bw.shopping.entity.GetAddressEntity;
import com.bw.shopping.entity.ListOrder;
import com.bw.shopping.entity.OrderEntity;
import com.bw.shopping.entity.PayEntity;
import com.bw.shopping.entity.SubOrderEntity;
import com.bw.shopping.entity.Subgoods;
import com.bw.shopping.entity.SubmitOrder;
import com.bw.shopping.mvvm.PostSticky;
import com.bw.shopping.mvvm.model.OrderModel;
import com.bw.shopping.mvvm.view.fragment.PayActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/29 0029 下午 1:37
 *
 */
class OrderViewModel extends BaseViewModel<OrderModel> {

    public ObservableField<String> totalPrice=new ObservableField<>();
    public ObservableField<String> username=new ObservableField<>();
    public ObservableField<String> address=new ObservableField<>();
    String phone;
    int addressid;
    int orderid;
    public BaseQuickAdapter adapter = new OrderAdapter();
    List<ListOrder> orders;
    OrderEntity.DataBean data;
    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
        requestOrder();
    }

    @Override
    protected void result(BaseEntity entity) {
        if (entity instanceof OrderEntity){
            OrderEntity orderEntity = (OrderEntity) entity;


            data = orderEntity.getData();

            orders = new ArrayList<>();
            for (int i = 0; i < data.getOrderGoodsList().size(); i++) {
                    ListOrder listOrder = new ListOrder(data.getOrderGoodsList().get(i).getGoods_icon(),
                            data.getOrderGoodsList().get(i).getGoods_id(),
                            data.getOrderGoodsList().get(i).getGoods_desc(),
                            data.getOrderGoodsList().get(i).getGoods_sku(),
                            data.getOrderGoodsList().get(i).getGoods_price(),
                            data.getOrderGoodsList().get(i).getGoods_count(),
                            data.getOrderGoodsList().get(i).getId(),
                            data.getOrderGoodsList().get(i).getOrder_id());
                    orders.add(listOrder);
                }

            String Price = data.getTotalPrice();

            Integer integer = Integer.valueOf(Price);
            totalPrice.set("￥："+integer/100);

            adapter.setNewInstance(orders);

        }else if (entity instanceof SubOrderEntity){
            SubOrderEntity subOrderEntity = (SubOrderEntity) entity;
            if (subOrderEntity.getMessage().equals("订单提交成功")){
                ToastUtils.showLong("订单提交成功");
                String Price = data.getTotalPrice();
                Integer integer = Integer.valueOf(Price);
                HashMap<String,Object>map = new HashMap<>();
                map.put("orderId",orderid);
                map.put("totalPrice",integer);
                m.requestPay(map)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io())
                        .subscribe(this);

            }else {
                ToastUtils.showLong("订单提交失败");
            }

        }else if (entity instanceof PayEntity){
            PayEntity payEntity = (PayEntity) entity;
            Bundle bundle = new Bundle();
            bundle.putString("sign",payEntity.getData());
            HashMap<String,Object>map = new HashMap<>();
            map.put(UIField.PAGECLAZZ, PayActivity.class);
            map.put(UIField.PAGEBUNDLE,bundle);
            startPage(map);
        }

    }
    @Subscribe(sticky = true)
    public void getAddress(PostSticky postSticky){

        String myusername = postSticky.myusername;
        username.set(myusername);
        String useraddress = postSticky.useraddress;
        address.set(useraddress);
        String mobile = postSticky.phone;
        phone = mobile;
        int id = postSticky.addressid;
        addressid = id;
    }



    public void requestOrder(){
        HashMap<String,Object> map = new HashMap<>();
        map.put("orderId",orderid);
        m.requestgetOrder(map)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);
    }
    //返回
    public void finish(){
        finishPage();
    }
    //提交订单
    public void subOrderListener(){
        int user_id = GreenDaoUtils.getGreenDaoUtils().green().queryBuilder().list().get(0).getUser_id();
        HashMap<String,Object> map= new HashMap<>();
        Subgoods subgoods = new Subgoods();
        ArrayList<Subgoods> list = new ArrayList<>();
        for (int i = 0; i < orders.size(); i++) {
            ListOrder order = orders.get(i);
            subgoods.setOrder_id(orderid);
            subgoods.setGoods_sku(order.getGoods_sku());
            subgoods.setGoods_count(order.getGoods_count());
            subgoods.setGoods_desc(order.getGoods_desc());
            subgoods.setGoods_icon(order.getGoods_icon());
            subgoods.setGoods_id(order.getGoods_id());
            subgoods.setGoods_price(order.getGoods_price());
            subgoods.setId(order.getId());
            list.add(subgoods);
        }
        map.put("id",data.getId());
        map.put("orderGoodsList",list);
        map.put("orderStatus",data.getOrderStatus());
        map.put("payType",data.getPayType());
        HashMap<String,Object> shipaddress = new HashMap<>();
        shipaddress.put("id",addressid);
        shipaddress.put("ship_address",address.get());
        shipaddress.put("ship_is_default",1);
        shipaddress.put("ship_user_mobile",phone);
        shipaddress.put("ship_user_name",username.get());
        shipaddress.put("user_id",user_id);
        map.put("shipAddress",shipaddress);
        map.put("totalPrice",data.getTotalPrice());
        HashMap<String,Object> sub = new HashMap<>();
        sub.put("order",map);
        sub.put("userId",user_id);
        m.requestsubOrder(sub)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(this);





    }
    public void getOrderId(int id){
        orderid = id;
    }

    @Override
    public void onDestory() {
        super.onDestory();
        EventBus.getDefault().unregister(this);
    }
}
